package com.teamide.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertyUtil {

	public static Properties get(String name) throws IOException {

		if (StringUtil.isEmpty(name)) {
			return null;
		}
		InputStream stream = ResourceUtil.load(name);
		return get(stream);
	}

	public static Properties get(InputStream stream) throws IOException {

		if (stream == null) {
			return null;
		}
		try {
			Properties props = new Properties();
			props.load(stream);
			return props;
		} finally {
			IOUtil.close(stream);
		}

	}

	public void save(File file, Properties properties) {

		FileOutputStream stream = null;
		try {
			if (!file.exists()) {
				if (!file.getParentFile().exists()) {
					file.getParentFile().mkdirs();
				}
				file.createNewFile();
			}
			stream = new FileOutputStream(file);
			properties.store(stream, null);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			IOUtil.close(stream);
		}
	}

}
